<?php
function trip_search_page_callback($page = 0) {
  global $language;
  $profile_type = substr($language->language, 0, 2);
  $keywords = check_plain($_GET['query']);
  $items = array();
  $items['keywords'] = $keywords;
//  if(!empty($keywords)) {
    $query = db_select("node", "n");
    $query->fields('n', array('nid', 'title', 'created', 'uid'));
    $query->addField("body", "body_value", "datail");
    $query->addField("fm", "uri");
    $query->addField("name", "field_name_value", "exp_name");
    $query->leftJoin('profile', 'up', "up.uid = n.uid AND up.type = '$profile_type'");
    $query->leftJoin("field_data_field_name", "name", "name.entity_id = up.pid AND name.deleted = 0");
    $query->leftJoin("field_data_field_exp_address", "ads", "ads.entity_id = n.nid AND ads.deleted = 0");
    $query->leftJoin("field_data_field_exp_photos", "photos", "photos.entity_id = n.nid AND photos.deleted = 0 AND photos.delta = 0");
    $query->innerJoin('file_managed', 'fm', 'fm.fid = photos.field_exp_photos_fid');
    $query->leftJoin("field_data_body", "body", "body.entity_id = n.nid AND body.deleted = 0");
    $query->leftJoin("field_data_field_approval", "approval", "approval.entity_id = n.nid AND approval.deleted = 0");
    $query->condition("approval.field_approval_value", 3);

    if(!empty($keywords)) {
      $db_or = db_or();
      $db_or->condition("ads.field_exp_address_value", '%'. db_like($keywords).'%', 'LIKE');
      $db_or->condition("body.body_value", '%'. db_like($keywords).'%', 'LIKE');
      $db_or->condition("n.title", '%'. db_like($keywords).'%', 'LIKE');
      $query->condition($db_or);
    }
    $offset = $page*6;
    $query->range($offset, 7);
    $result = $query->execute();
    $exp_list = array();
    while ($record = $result->fetchObject()) {
      $exp_list[] = $record;
    }
    $items['data'] = $exp_list;
    $items['page'] = ++$page;
    $items['keywords'] = $keywords;
//  }
  drupal_add_css(drupal_get_path("module", "trip_search") . "/css/trip_search.css");
  return theme("trip_search_page", array('items' => $items));
}
